/* **************************************************************
NAME: yemenattritiontable.ado   
CREATED: 26 February 2021
EDITED: 20 September 2021 
AUTHOR: callan.corcoran@kellogg.northwestern.edu
PURPOSE: Program to create attrition tables for the Yemen project 
************************************************************** */

cap prog drop yemenattritiontable

prog def yemenattritiontable
syntax varlist [if/], 	/// Variables to be included in table 
	[filename(string)] 		/// File name (default is TableX)
	[foldername(string)]	/// Output folder name (default is Replication/Output)
	[title(string)] 		/// Table title 
	[footnote(string)] 		/// Footnote text 
	[winsorize] 			/// Winsorize variables 
	[fixedeffects(varlist)] /// Set fixed effects (default is village) 
	[stderrors(string)] 	/// Set standard errors (default is robust)
	[scale]					/// Scale variables 
	[ftest]					/// Run supplemental F-tests 


// **************************************************************
// PREPARE TABLE INPUTS 	

/*
loc varlist num_adults_bsl nb_children_bsl hhsize_bsl avg_age_bsl avg_edu_years_bsl gender_head_bsl age_head_bsl hh_head_over60_bsl hh_head_educ_bsl disabl_head_bsl /// Table 1 variables 
		asset_index_bsl ctotalpc_bsl ctotalhh_bsl inc_total_bsl ls_hany_bsl inonagriself_bsl fs_index_bsl savings_bsl percep_econ_bsl hous_sindex_bsl debt_index_bsl 
		
loc if "geo_cluster==1"
loc filename "TableX_Attrition_Cluster1" 
loc title "Appendix Table 2: Endline Survey Response Rate Analysis"
loc winsorize winsorize
loc foldername "$output_temp"
loc scale scale // ftest
loc ftest ftest
*/
 

// FOLDER NAME 
// 	Set default folder to Replication/Output if not specified 
if "`foldername'"=="" {
	loc foldername "${rep_output}" 
}

// FILE NAME 
// 	Set a default filename 
if "`filename'"=="" {
	loc filename "Table1"
}
if "`winsorize'"=="winsorize" {
	loc filename "`filename'_win"
}

// WINSORIZE 
//	Edit varlist to include winsorized variable names if "winsorize" selected 
if "`winsorize'"=="winsorize" {
    loc templist `varlist'
	loc varlist // Clear varlist 
	foreach var in `templist' {
		loc currvar = subinstr("`var'","_bsl","_win1_bsl",.) // Add "_win1" suffix 
		loc varlist `varlist' `currvar' // Add to varlist if "winsorize" is selected
	}
}

// FIXED EFFECTS 
// 	Set fixed effects to village, if not specified 
if "`fixedeffects'"=="" {
	loc fixedeffects village 
}

// STANDARD ERRORS 
//	Default standard errors to robust 
if "`stderrors'"=="" {
	loc stderrors robust 
}

// IF-STATEMENT 
if "`if'"!="" {
    loc ifif "if `if'"
	loc andif "& `if'"
} 

// FOOTNOTES 
// 	Add footnotes about baseline values and additional controls
if "`winsorize'"=="winsorize" {
	loc footnote "`footnote' Variables winsorized at 1%."
}

// **************************************************************
// PREP VARAIBLES 

use "${rep_analysis}.dta", clear 

// SCALING 
// Scale variables to make effect sizes visible 
if "`scale'"=="scale" {
	foreach wins in "" "_win1" {
	    replace avg_age`wins'_bsl = avg_age`wins'_bsl / 10 
			la var avg_age`wins'_bsl "Average Age / 10"
		
		replace age_head`wins'_bsl = age_head`wins'_bsl / 10 
			la var age_head`wins'_bsl "HH Head Age / 10" 
			
		replace hh_head_educ`wins'_bsl = hh_head_educ`wins'_bsl / 10 
			la var hh_head_educ`wins'_bsl "HH Head Years Edu / 10" 
			
		replace asset_index`wins'_bsl = asset_index`wins'_bsl / 100 
			la var asset_index`wins'_bsl "Asset Index / 100"
			
		replace ctotalpc`wins'_bsl = ctotalpc`wins'_bsl / 1000
			la var ctotalpc`wins'_bsl "PC Consumption / 1000"
			
		replace ctotalhh`wins'_bsl = ctotalhh`wins'_bsl / 1000 
			la var ctotalhh`wins'_bsl "HH Consumption / 1000"
			
		replace inc_total`wins'_bsl = inc_total`wins'_bsl / 1000 
			la var inc_total`wins'_bsl "Total Income / 1000"
			
/*
		replace swf_income`wins'_bsl = swf_income`wins'_bsl / 1000 
			la var swf_income`wins'_bsl "SWF Income / 1000" 
*/
			
		replace percep_econ`wins'_bsl = percep_econ`wins'_bsl / 10 
			la var percep_econ`wins'_bsl "Economic Status Ranking / 10"
	}
}

// Create placeholder locals 
loc treat_int_vars
loc missing_attrition_vars
loc missing_attrition_vars_int

// 1. Demean variables
foreach var in `varlist' {
	qui sum `var' if treatment == 0
	replace `var' = `var' - `r(mean)' if !missing(`var')
}

// 2. Interact variables 
//	Create interaction terms, missings, and interacted missings
foreach var in `varlist' {
	dis in r "`var'"

	// Create missings

	loc missing_attrition_vars `missing_attrition_vars' m_`var'
	
	// Replace as 0 if missing
	replace `var' = 0 if missing(`var')

	// Create interaction vars
	gen `var'Xtreat = `var'*treatment
	local varlabel : variable label `var'
	loc varlabel = "`varlabel' " + "X Treatment"
	lab var `var'Xtreat "`varlabel'"
	
	loc treat_int_vars `treat_int_vars' `var'Xtreat
	
	*** Create missing interaction vars
	gen m_`var'Xtreat = m_`var'*treatment
	local varlabel : variable label m_`var'
	loc varlabel = "`varlabel' Missing (1/0)" + "X Treatment"
	lab var m_`var'Xtreat "`varlabel'"
	
	loc missing_attrition_vars_int `missing_attrition_vars_int' m_`var'Xtreat
	
}

// 3. Group variables using locals 
loc empty					
loc attrition_vars 			`varlist' 
loc attrition_int_vars 		`varlist' `treat_int_vars'

loc empty_miss
loc attrition_vars_miss		`missing_attrition_vars'
loc attrition_int_vars_miss `missing_attrition_vars' `missing_attrition_vars_int'

tempfile attrition_set 
	save `attrition_set'

// **************************************************************
// CREATE TABLE FRAMEWORK 

clear
eststo clear
estimates drop _all

loc columns = 6

*** Create blank table
set obs 10
gen x = 1
gen y = 1

forval i = 1/`columns' {
	eststo col`i': reg x y
}

*** Set locals for table
loc var_num : word count treatment `attrition_vars' `treat_int_vars'
dis in r "`var_num'"
loc ++var_num 
loc dep_mean = `var_num'
loc ++var_num 
loc obs = `var_num'
loc ++var_num 
loc ++var_num 
loc test1 = `var_num'
loc ++var_num 
loc test2 = `var_num'
loc ++var_num 
loc test3 = `var_num'
loc ++var_num 

loc stats "" 			// Added scalars to be filled
loc varlabels "" 			// Labels for row vars to be filled

loc format %9.2f

use `attrition_set', clear 

// **************************************************************
// FILL TABLE CELLS 

loc firstcol = 1
loc seccol = 2

foreach set in empty attrition_vars attrition_int_vars {

dis in r "`firstcol'"
dis in r "`seccol'"

	// Main Regression 
	qui areg surveyed ``set'' ``set'_miss' treatment `ifif', absorb(`fixedeffects') vce(`stderrors') // Attrition regression 
	mat R = r(table)

	// Tests 
	// Treatment only
	if `firstcol' == 1 { 
		qui test treatment 
		estadd loc stat`test1' = string(r(p), "`format'") : col`firstcol'
	}
	// Treatment and variables 
	if `firstcol' == 3 { 
		qui test treatment 
		estadd loc stat`test1' = string(r(p), "`format'") : col`firstcol'
				
	}
	// Treatment and variables and interactions 
	if `firstcol' == 5 { 
		qui test treatment 
		estadd loc stat`test1' = string(r(p), "`format'") : col`firstcol'
		
		test `treat_int_vars' `missing_attrition_vars_int'
		estadd loc stat`test2' = string(r(p), "`format'") : col`firstcol'
	
		test treatment `treat_int_vars' `missing_attrition_vars_int'
		estadd loc stat`test3' = string(r(p), "`format'") : col`firstcol'
	}
			
		
	// Save observations and control mean 
	sum surveyed if e(sample)==1
	estadd loc stat`obs' = string(r(N), "%9.0f") : col`firstcol'
	sum surveyed if e(sample)==1 & treatment==0
	estadd loc stat`dep_mean' = string(r(mean), "`format'") : col`firstcol'

	// Save results for interaction terms
	loc count = 1
	foreach var in treatment ``set'' {

	// Column 1: Coefficient
		loc coef = el(R,rownumb(R,"b"),colnumb(R,"`var'"))
		estadd loc stat`count' = string(`coef',"`format'") : col`firstcol' //Coefficient on interaction term from attrition regression
	
	// Column 2: Bracketed standard error with stars 
		loc pval = el(R,rownumb(R,"pvalue"),colnumb(R,"`var'"))
			if `pval' < 0.01 {
				loc star "***"
			}
			else if `pval' < 0.05 {
				loc star "**"
			}
			else if `pval' < 0.1 {
				loc star "*"
			}
			else {
				local star ""
			}

		loc se = el(R,rownumb(R,"se"),colnumb(R,"`var'"))
		loc stat`count' = string(`se',"`format'")	
		estadd loc stat`count' = "(" + "`stat`count''" + ")" + "`star'": col`seccol' //Add standard error with stars from attrition regression
	
	if `firstcol' == 5 {
		// Label rows and update locals
		loc thisvarlabel: variable label `var' // Extracts label from row var
		local varlabels "`varlabels' "`thisvarlabel'" "
		
	}
	loc ++count
	}
	
	loc ++firstcol
	loc ++firstcol
	loc ++seccol
	loc ++seccol
}

loc varlabels "`varlabels' "Mean of dependent Variable" "Observations" "p-value of tests:" "Treatment = 0" "Interacted covariates = 0" "Treatment & Interacted covariates = 0"" // Label variables

// Set stats to include in table
forv i = 1/`var_num' {
	loc stats "`stats' stat`i' "
}

//***********************************************************
// EXPORT TABLE 

cd "`foldername'" // Call output folder directory 

esttab using "`filename'.csv",  title("`title'") cells(none) ///
nonum mtitle("Coeff." "Std. Err" "Coeff." "Std. Err" "Coeff." "Std. Err") stats(`stats',labels(`varlabels')) note("`footnote'")  ///
compress wrap replace

//***********************************************************
// SUPPLEMENTAL F-TESTS 


if "`ftest'" == "ftest" {
	// Head age 
	loc agesetnote "F-Test with HH Head Age Variables"
	loc ageset age_head_win1_bsl hh_head_over60_win1_bsl 
	
	// All head characteristics 
	loc headsetnote "F-Test with HH Head Demographic Variables"
	loc headset gender_head_win1_bsl age_head_win1_bsl hh_head_over60_win1_bsl hh_head_educ_win1_bsl disabl_head_win1_bsl 
	
	// All demographics 
	loc demosetnote "F-Test with All Demographic Variables"
	loc demoset num_adults_win1_bsl nb_children_win1_bsl hhsize_win1_bsl avg_age_win1_bsl avg_edu_years_win1_bsl gender_head_win1_bsl age_head_win1_bsl hh_head_over60_win1_bsl hh_head_educ_win1_bsl disabl_head_win1_bsl
	
	// All welfare outcomes 
	loc welfsetnote "F-Test with All Welfare Variables"
	loc welfset asset_index_win1_bsl ctotalpc_win1_bsl ctotalhh_win1_bsl inc_total_win1_bsl ls_hany_win1_bsl inonagriself_win1_bsl fs_index_win1_bsl savings_win1_bsl percep_econ_win1_bsl hous_sindex_win1_bsl debt_index_win1_bsl // Replacing swf_income_win1_bsl with inonagriself_win1_bsl
	
	// All outcomes 
	loc allsetnote "F-Test with All Outcome Variables"
	loc allset `demoset' `welfset'

	foreach set in ageset headset demoset welfset allset { // Loop through each test specification 

		foreach var in ``set'' { 
			loc `set'test ``set'test' `var'Xtreat m_`var'Xtreat // Create a test set of interacted-with-treatment variables 
			loc `set'additional ``set'additional' `var' m_`var' // Create a set of levels and missing dummies 
		}
		
		// Test the characteristic and interaction set 
		di in r "``set'note'"
		qui areg surveyed ``set'test' ``set'additional' treatment `ifif', absorb(village) vce(robust) // Attrition regression 
			test ``set'test'
	}
}

end 

